%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%The constraint function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [c, ceq] = mycon(input)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Ds=input(1);
Be=input(2);
delta=input(3);
Bd=input(4);
Bcs=input(5);
C=20;omega=721*pi/30; omegamax=1442*pi/30;

% Propriitis des matiriaux
kr=0.5;dcu=8950;kfoi=0.95;dt=7850;dcr=7850;mua=1.05;
kfui=0.8;Br=1.045;qt=2.5;ft=50; Bt=1.5;rocu=1.72e-8;
Pm=15;da=7400;muo=4*pi*1e-7;Bc=0.05;

% Paramhtres d'entrée ` trouver
Lm=45e-3;

% Paramhtres d'entrie fixis
e=0.8e-3; Udc=120; Bcr=1.2; rrs=1.11; p=6; Ne=3/2*p;

% Propriitis des matiriaux pour la thermique 
alphaa=-5e-4; alphacu=3.8e-3; Text=50; h=10; 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%Calcul de la masse: Mtot
% Equations dimensionnantes
alpha=pi/p;
beta=pi/p;
alphai=alpha/5;
li=Be/Bd*alphai*Ds/2;
ld=Be/Bd*alpha*Ds/2;
eb=(Be/Bd*(alpha/2-asin(ld/Ds))*Ds/2)+Ds/2*(1-cos(asin(ld/Ds))); 
kv=omegamax/omega;
E=Udc/2/kv;
n=4*E/Be/Ds/Lm/omega;
I=C*omega/2/E;
Senc=3/2*n*I/delta/kr;
% calcul discriminant pour hauteur dent
a=pi;
b=-(2*pi*(Ds/2-eb)-Ne*(li+ld));
c=Senc;
discr=b^2-4*a*c;
hd=(-b-sqrt(abs(discr)))/2/a;% calcul hauteur dent
Rtb=((ld-li)/4+(Ds/2-eb-hd/2)*pi/2/Ne);
Lds=(Lm/kfoi+pi*Rtb);

Mcu=dcu*3/2*n*I/delta*Lds;
hc=(eb/cos(alpha/2)-Ds/2*(1/cos(alpha/2)-1));
hi=(Ds/2*(1-cos(alphai/2))+hc*cos(alphai/2));
Mds=dt*Ne*((ld+li)*hd+Ds/2*(alphai*(hi+hc)/2+alpha*(eb+hc)/2))*Lm;
hcs=1/2*Bd/Bcs*ld;
Mcs=dt*pi*hcs*(-hcs+2*(Ds/2-eb-hd))*Lm;
     Ba=Be*alpha*Ds/2/beta/(Ds/2+e)/rrs/kfui;
     f=p*omega/2/pi; 
     Pf=qt*((f/ft)^1.5)*(Mcs*((Bcs/Bt)^2)+Mds*((Bd/Bt)^2)); 
     hcr=1/2*Ba/Bcr*beta*(Ds/2+e); 
 Ltot=(Lm/kfoi+2*((Ds/2-eb-hd/2)*pi/Ne-li/2));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% systhme d'iquations implicites 
     Ta=100; Tcu=110;
    while abs(Ta-Tcu)>1e-6 
Ta=Tcu; 
ha=-mua*Be*e/(Ba-Br*(1+alphaa*Ta)); 
%%%%Calcul de : Dext%%%%%%%%%%
Dext=Ds+2*(e+ha+hcr); 
%%%%Fin calcul de : Dext%%%%%%
Rph=rocu*(1+alphacu*Tcu)*n/2*Lds*delta/I; 
Pj=2*Rph*I^2; 
Sext=pi/2*(Dext^2)+pi*Dext*Ltot; 
Tcu=Text+(Pj+Pf+Pm)/h/Sext; 
    end 
% fin systhme d'équations implicites 
Mcr=dcr*pi*hcr*(hcr+2*(Ds/2+e+ha))*Lm*rrs;
Ma=da*p*beta*ha*(ha+2*(Ds/2+e))*Lm*rrs;
Mtot=Ma+Mcr+Mcs+Mds+Mcu;
c(1)=(Mtot/15)-1;%=
%%%%%%%%%%% Fin calcul de la masse: Mtot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Calcul de: Imax
Imax=-4*p/n*(Bc/muo*beta/alpha*(1+2*e/Ds)*rrs*kfui*e+(Bc-Br*(1+alphaa*Ta))*ha/muo/mua);
c(2)=1-(Imax/125);
%%%%%%%%%% Fin calcul Imax
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Calcul de: Dint
Dint=Ds-2*(eb+hd+hcs);
c(3)= 1-(Dint/76e-3);%=Dint
%%%%%%%%%% Fin calcul Dint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Calcul de: Dext
c(4)=(Dext/340e-3)-1; %=Dext
%%%%%%%%%% Fin calcul Dext
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
c(5)=-discr+1e-6;
c(6)=(Ta/120)-1;

ceq=0;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% 
%          End of the constraint function
%%%%%%%%%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%




